/**
 * File: BufferedRafChannelFactory.java
 * Created by: mhaimel
 * Created on: 10 Aug 2009
 * CVS:  $Id: BufferedRafChannelFactory.java,v 1.2 2009/10/09 15:16:53 mhaimel Exp $
 */
package uk.ac.ebi.curtain.util.file.impl;

import uk.ac.ebi.curtain.util.file.impl.FileAccessPool.ChannelType;
import uk.ac.ebi.curtain.utils.CurtainUncheckedException;
import uk.ac.ebi.curtain.utils.io.impl.BufferedRafReader;
import uk.ac.ebi.curtain.utils.io.impl.FileIO;

/**
 * @author mhaimel
 *
 */
public class BufferedRafChannelFactory extends AbstractFactory {

	private static final ChannelType RAF_BUFF = ChannelType.RAF_Buff;

	/**
	 * {@inheritDoc}
	 */
	public ChannelType getType() {
		return RAF_BUFF;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void _doActivateObject(FilePoolInfo info, ChannelWrapper cin)throws Exception { 
		BufferedRafReader reader = cin.getChannel();
		reader.reset();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void _doPassivateObject(FilePoolInfo info, ChannelWrapper cin) throws Exception { /* nothing */}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void _openChannel(FilePoolInfo info, ChannelWrapper wrapper)
			throws Exception {
		if(!info.getRead()){
			throw new CurtainUncheckedException("Buffered Reader not supported!!! " + info);
		} 
		wrapper.setChannel(new FileIO(info.getFile(),true).getBufferedRafReader());		
	}	
}
